package com.lepu.app.fun.index.adapter;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.app.utils.DateUtil;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.ui.wheel.WheelView;
import com.core.lib.widget.MyDialog;
import com.lepu.app.fun.index.activity.IndexRemindAddActivity;
import com.lepu.pasm.R;
public class IndexRemindAddAdapter extends BaseAdapter {
	private LayoutInflater mLayoutInflater = null;
	private Context mContext = null;
	private ArrayList<String> mArrayList = null;

	public IndexRemindAddAdapter(Context context,ArrayList<String> arrayList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			
		    convertView = mLayoutInflater.inflate(R.layout.index_remind_add_item, null);

			holder = new ViewHolder();

			TextView timeTextView = (TextView) convertView.findViewById(R.id.timeText);
			TextView deleteTextView = (TextView) convertView.findViewById(R.id.deleteText);

			holder.timeTextView = timeTextView;
			holder.deleteTextView = deleteTextView;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final String item = mArrayList.get(position);
		
		if(position == 0){
			holder.deleteTextView.setVisibility(View.GONE);
		}else{
			holder.deleteTextView.setVisibility(View.VISIBLE);
		}
		
		holder.timeTextView.setText(item);
		holder.timeTextView.setOnClickListener(new MyViewOnclicklistener(holder.timeTextView, position));
		
		holder.deleteTextView.setOnClickListener(new MyViewOnclicklistener(position));

		return convertView;
	}
	
	  class MyViewOnclicklistener implements View.OnClickListener {
	        private TextView textView;
	        private int position;

	        public MyViewOnclicklistener(TextView textView, int position) {
	            this.textView = textView;
	            this.position = position;
	        }
	        public MyViewOnclicklistener(int position) {
	        	this.position = position;
	        }
	        

	        @Override
	        public void onClick(View v) {
	            final int rid = v.getId();
	            switch (rid) {
	                case R.id.timeText: //点击添加时间
	                    String dateStr = textView.getText().toString().trim();
	                    if (dateStr.equals("")) {
	                        dateStr = DateUtil.getNowDate("HH:mm");
	                    }
	                    Date date = DateUtil.dateFromString(dateStr, "HH:mm");
	                    MyDialog dialog = new MyDialog((BaseFragmentActivity)mContext).setTitle("请选择时间")
	                            .setTimePicker(date).setNegativeButton(R.string.app_cancel, null)
	                            .setPositiveButton(R.string.app_ok, new View.OnClickListener() {
	                                @Override
	                                public void onClick(View v) {
	                                    WheelView wv_hour = (WheelView) v
	                                            .findViewById(R.id.hour);
	                                    WheelView wv_minute = (WheelView) v
	                                            .findViewById(R.id.minute);
	                                    String parten = "00";
	                                    DecimalFormat decimal = new DecimalFormat(parten);
	                                    String text = decimal.format(wv_hour.getCurrentItem())
	                                            + ":"
	                                            + decimal.format(wv_minute.getCurrentItem());
	                                    
	                                    textView.setText(text);
	                                    mArrayList.set(position, text);
	                                }
	                            });
	                    dialog.create(null).show();
	                    break;
	                case R.id.deleteText:
	                	String message = "您确定删除该提醒吗？";
	            		MyDialog dialog1 = new MyDialog((BaseFragmentActivity)mContext)
	            				.setMessage(message)
	            				.setNegativeButton(R.string.app_cancel,
	            						R.drawable.btn_cancel_selector, R.color.color_white,
	            						new View.OnClickListener() {

	            							@Override
	            							public void onClick(View v) {

	            							}
	            						})
	            				.setPositiveButton(R.string.app_ok,
	            						R.drawable.btn_ok_selector, R.color.color_white,
	            						new View.OnClickListener() {

	            							@Override
	            							public void onClick(View v) {
	            								IndexRemindAddActivity.getInstance().deleteItem(position);
	            							}
	            						});

	            		dialog1.create(null).show();
	                	
	                	break;
	                default:
	                    break;
	            }
	        }
	    }
	

	private static class ViewHolder {
		TextView timeTextView;
		TextView deleteTextView;
	}
}
